package com.wlcorp.forcazamis.business.storageassign.impl;

import java.util.Iterator;
import java.util.List;

import com.wlcorp.forcazamis.business.storageassign.IStorageAssignService;
import com.wlcorp.forcazamis.dao.baseinfo.IDepotDAO;
import com.wlcorp.forcazamis.dao.confirm.IConfirmDAO;
import com.wlcorp.forcazamis.dao.sample.ISubSampleAssignDAO;
import com.wlcorp.forcazamis.dao.sample.ISubSampleDAO;
import com.wlcorp.forcazamis.dao.storageassign.IStorageAssignDAO;
import com.wlcorp.forcazamis.dao.storageassign.IStorageAssignSampleDAO;
import com.wlcorp.forcazamis.dao.system.IStaffDAO;
import com.wlcorp.forcazamis.domain.baseinfo.Depot;
import com.wlcorp.forcazamis.domain.sample.SubSampleAssign;
import com.wlcorp.forcazamis.domain.storageassign.StorageAssign;
import com.wlcorp.forcazamis.domain.storageassign.StorageAssignSample;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;

public class StorageAssignServiceImpl extends BaseService implements
		IStorageAssignService {
	
	private IStorageAssignDAO storageAssignDAO;
	private IStorageAssignSampleDAO storageAssignSampleDAO;
	private ISubSampleDAO subSampleDAO;
	private IDepotDAO depotDAO;
	private IStaffDAO staffDAO;
	@SuppressWarnings("unused")
	private IConfirmDAO confirmDAO;
	private ISubSampleAssignDAO subSampleAssignDAO;
	public void setStorageAssignDAO(IStorageAssignDAO storageAssignDAO) {
		this.storageAssignDAO = storageAssignDAO;
	}
	
	public void setStorageAssignSampleDAO(IStorageAssignSampleDAO storageAssignSampleDAO) {
		this.storageAssignSampleDAO = storageAssignSampleDAO;
	}
	
	public void setDepotDAO(IDepotDAO depotDAO) {
		this.depotDAO = depotDAO;
	}

	public void setSubSampleDAO(ISubSampleDAO subSampleDAO) {
		this.subSampleDAO = subSampleDAO;
	}
	
	public void setStaffDAO(IStaffDAO staffDAO) {
		this.staffDAO = staffDAO;
	}
	
	
	public void setConfirmDAO(IConfirmDAO confirmDAO) {
		this.confirmDAO = confirmDAO;
	}

	public void setSubSampleAssignDAO(ISubSampleAssignDAO subSampleAssignDAO) {
		this.subSampleAssignDAO = subSampleAssignDAO;
	}

	public List<StorageAssign> getStorageAssignList(StorageAssign storageAssign) throws ServiceException {
		return storageAssignDAO.getStorageAssignList(storageAssign);
	}

	public Long getStorageAssignCount(StorageAssign storageAssign) throws ServiceException {
		return storageAssignDAO.getStorageAssignCount(storageAssign);
	}

	public StorageAssign getStorageAssignById(Long storageAssignId) throws ServiceException {
		if(null == storageAssignId){
			return null;
		}
		return storageAssignDAO.getStorageAssignById(storageAssignId);
	}

	public void deleteStorageAssign(StorageAssign storageAssign) throws ServiceException {
		storageAssignDAO.deleteStorageAssign(storageAssign);
	}

	public void saveStorageAssign(StorageAssign storageAssign) throws ServiceException {
//		boolean flag = false;
//		if(null == storageAssign.getStorageAssignId()){
//			flag = true;
//		}
		if(null != storageAssign.getAssignFromDepotId()){
			storageAssign.setAssignFromDepot(depotDAO.getDepotById(storageAssign.getAssignFromDepotId()));
		}
		if(null != storageAssign.getAssignToDepotId()){
			storageAssign.setAssignToDepot(depotDAO.getDepotById(storageAssign.getAssignToDepotId()));
		}
		if(null != storageAssign.getAssignToStaffId()){
			storageAssign.setAssignToStaff(staffDAO.getStaffById(storageAssign.getAssignToStaffId()));
		}
		
		storageAssignDAO.saveStorageAssign(storageAssign);
//		if(flag){//新增时自动增加审核信息
//			Confirm confirm = new Confirm();
//			confirm.setConfirmType(AppConstant.CONFIRM_TYPE_ASSIGN_STORAGE);
//			confirm.setSubmitTime(storageAssign.getAssignDate());
//			confirm.setSubmiter(storageAssign.getSubmiter());
//			confirm.setItemId(storageAssign.getStorageAssignId());
//			confirm.setState(AppConstant.CONFIRM_STATE_CONFIRMING);
//			confirm.setNextConfirmer(storageAssign.getAssignToStaff());
//			confirmDAO.saveConfirm(confirm);
//		}
	}

	public List<StorageAssignSample> getStorageAssignSampleList(StorageAssignSample storageAssignSample) throws ServiceException {
		return storageAssignSampleDAO.getStorageAssignSampleList(storageAssignSample);
	}

	public Long getStorageAssignSampleCount(StorageAssignSample storageAssignSample) throws ServiceException {
		return storageAssignSampleDAO.getStorageAssignSampleCount(storageAssignSample);
	}

	public void saveStorageAssignSample(List<StorageAssignSample> storageAssignSampleList) throws ServiceException {
		StorageAssign storageAssign = storageAssignDAO.getStorageAssignById(((StorageAssignSample)storageAssignSampleList.get(0)).getStorageAssignId());
		if(null != storageAssign.getStorageAssignSampleSet()){
			for(Iterator<?> iter = storageAssign.getStorageAssignSampleSet().iterator();iter.hasNext();){
				StorageAssignSample storageAssignSample = (StorageAssignSample)iter.next();
				storageAssignSampleDAO.deleteStorageAssignSample(storageAssignSample);
			}
		}
		for(Iterator<StorageAssignSample> iter = storageAssignSampleList.iterator();iter.hasNext();){
			StorageAssignSample storageAssignSample = (StorageAssignSample)iter.next();
			storageAssignSample.setSubSample(subSampleDAO.getSubSampleById(storageAssignSample.getSubSampleId()));
			storageAssignSample.setStorageAssign(storageAssign);
			storageAssignSampleDAO.saveStorageAssignSample(storageAssignSample);
		}
	}

	public void withDrawStorageAssign(StorageAssign storageAssign) throws ServiceException {
		Depot fromDepot = storageAssign.getAssignFromDepot();
		Depot toDepot = storageAssign.getAssignToDepot();

		for(Iterator<?> iter = storageAssign.getStorageAssignSampleSet().iterator();iter.hasNext();){
			StorageAssignSample storageAssignSample = (StorageAssignSample)iter.next();
			SubSampleAssign fromSubSampleAssign = new SubSampleAssign();
			fromSubSampleAssign.setSubSampleId(storageAssignSample.getSubSample().getSubSampleId());
			fromSubSampleAssign.setOwnerId(fromDepot.getDepotId());
			
			fromSubSampleAssign = subSampleAssignDAO.getSubSampleAssignBySubSampleAndOwner(fromSubSampleAssign);
			
			SubSampleAssign toSubSampleAssign = new SubSampleAssign();
			toSubSampleAssign.setSubSampleId(storageAssignSample.getSubSample().getSubSampleId());
			toSubSampleAssign.setOwnerId(toDepot.getDepotId());
			toSubSampleAssign = subSampleAssignDAO.getSubSampleAssignBySubSampleAndOwner(toSubSampleAssign);
			
			if(null == toSubSampleAssign || null == toSubSampleAssign.getSampleCount() ||
					toSubSampleAssign.getSampleCount() < storageAssignSample.getAssignCount()){
				setErrorField(storageAssign,AppConstant.BASE_BEAN_STATUS_ERROR,"样品颜色“" +  storageAssignSample.getSubSample().getCcolorDesc() 
						+ "”的样品剩余库存少于" + storageAssignSample.getAssignCount() + "个，无法推单！");
				return;
			}
			fromSubSampleAssign.setSampleCount(fromSubSampleAssign.getSampleCount() + storageAssignSample.getAssignCount());
			toSubSampleAssign.setSampleCount(toSubSampleAssign.getSampleCount() - storageAssignSample.getAssignCount());
			subSampleAssignDAO.saveSubSampleAssign(fromSubSampleAssign);
			subSampleAssignDAO.saveSubSampleAssign(toSubSampleAssign);
		}
		storageAssign.setState(AppConstant.BILL_STATE_DRAWBACK);
		storageAssignDAO.saveStorageAssign(storageAssign);
	}

}
